//
//  YTSelectCarViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/8/8.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTSelectCarViewController.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>
#import "YTPickerCarViewCell.h"
#import "YTDriverCarServices.h"
#import "YTSelecteCarViewModel.h"

@interface YTSelectCarViewController ()<UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (weak, nonatomic) IBOutlet UIButton *leftBtn;
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;
@property (weak, nonatomic) IBOutlet UIImageView *leftIcon;
@property (weak, nonatomic) IBOutlet UIImageView *rightIcon;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) YTSelecteCarViewModel *viewModel;
@end

@implementation YTSelectCarViewController

- (void)initialize {
    _viewModel = [[YTSelecteCarViewModel alloc] init];
    _tableView.rowHeight = 110;
    [_tableView registerNib:[UINib nibWithNibName:@"YTPickerCarViewCell" bundle:nil]
     forCellReuseIdentifier:@"YTPickerCarViewCell"];
    [self dataDispatch];
}

- (void)bindViewModel {
    [self.viewModel.requestRemoteDataCommand execute:nil];
    @weakify(self);
    [_tableView addLoadDataControl:^{
        @strongify(self);
        [self.viewModel.loadMoreDataCommand execute:nil];
    }];
}

- (void)dataDispatch {
    @weakify(self);
    [[_viewModel.requestRemoteDataCommand.executionSignals switchToLatest] subscribeNext:^(id x) {
         @strongify(self);
         [self.tableView reloadData];
     }];
    [[_viewModel.changTypeCmd.executionSignals switchToLatest] subscribeNext:^(id x) {
        @strongify(self);
        [self.tableView reloadData];
    }];
    [_viewModel.changTypeCmd.executing subscribeNext:[self yt_showHudWithTips:nil]];
    [_viewModel.requestRemoteDataCommand.executing subscribeNext:[self yt_showHudWithTips:nil]];
    [[_viewModel.loadMoreDataCommand.executionSignals switchToLatest] subscribeNext:^(NSArray *data) {
        @strongify(self);
        if (data.count) {
            [self.tableView reloadData];
        } else {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

#pragma mark - UITableViewDelegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YTPickerCarViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YTPickerCarViewCell" forIndexPath:indexPath];
    if (indexPath.row <= _viewModel.dataArray.count) {
        cell.model = _viewModel.dataArray[indexPath.row];
    }
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _viewModel.dataArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row <= _viewModel.dataArray.count) {
        [_callBack sendNext:_viewModel.dataArray[indexPath.row]];
        [_callBack sendCompleted];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = @"当前暂无相关车辆";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0f],
                                 NSForegroundColorAttributeName: kGrayColor};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"empty_car"];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -100;
}

#pragma mark - 左右按钮点击

- (IBAction)leftBtnAction:(UIButton *)sender {
    [self changeBtn:sender otherBtn:_rightBtn icon:_leftIcon tabClass:@"carClass"];
}

- (IBAction)rightBtnAction:(UIButton *)sender {
    [self changeBtn:sender otherBtn:_leftBtn icon:_rightIcon tabClass:@"maxPassenger"];
}

- (void)changeBtn:(UIButton *)btn otherBtn:(UIButton *)otherBtn icon:(UIImageView *)icon tabClass:(NSString *)tabClass {
    [btn setTitleColor:kMainBlueColor forState:UIControlStateNormal];
    [otherBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
    
    if ([icon isEqual:_leftIcon]) {
        _rightIcon.image = [UIImage imageNamed:@"order_btn_none"];
    } else {
        _leftIcon.image = [UIImage imageNamed:@"order_btn_none"];
    }
    
    if ([icon.image isEqual:[UIImage imageNamed:@"order_btn_top"]]) {
        icon.image = [UIImage imageNamed:@"order_btn_bottom"];
        [_viewModel.changTypeCmd execute:RACTuplePack(tabClass, @"0")];
        return;
    }
    
    if ([icon.image isEqual:[UIImage imageNamed:@"order_btn_none"]] || [icon.image isEqual:[UIImage imageNamed:@"order_btn_bottom"]]) {
        icon.image = [UIImage imageNamed:@"order_btn_top"];
        [_viewModel.changTypeCmd execute:RACTuplePack(tabClass, @"1")];
        return;
    }
}

@end
